﻿
function ProcessUIToolbar()  
{ 
//    var Factory = window.templates
    var dis=window.http.readyState < ReadyState.DONE||!window.loggedinuser.PERM_BROWSE?'disabled':''
	var data=Toolbar.read ();
	var stamp = '/stamp/' + new Date().getTime();
	
	registerTemplate ('controlLink', Toolbar.controlLink);
	registerTemplate ('optionRow', Toolbar.optionRow);
	registerTemplate ('fieldForm', Toolbar.fieldForm);  
	
    var object  = { 'text' : 'Events', 'template' : 'controlLink' };
    var toolbar = { 'status' : '', 'enable' : 'enabled' };
    
    var htm = ''; 
	

// ---------------------------------------------------------------------- *  
    object = { 
				'text' : 'Events', 
				'weight' : window.mvcevent.indexOf('event')==0 ? 'bold' : 'normal', 
//				'onclick' :  window.mvcevent.indexOf('event')==0 ? 'return false' : 'HomePageEvent.invoke()', 
				'onclick' :  'void(0)', 
				'href' : '#view/eventlist', 
				'color' :  window.mvcevent.indexOf('event')==0 ? '#000' : '#0000ff',  
				'template' : 'controlLink'
			};
	object['href'] += stamp;
    toolbar.eventlink = Factory.write (object);  
// ---------------------------------------------------------------------- *  
    
// ---------------------------------------------------------------------- *  
    object = { 
				'text' : 'Rooms', 
				'weight' : window.mvcevent=='roomlist' ? 'bold' : 'normal', 
//				'onclick' :  window.loggedinuser.PERM_EDIT_ROOM && window.mvcevent!='roomlist' ? 'RoomListEvent.invoke()' : '', 
				'onclick' :  'void(0)', 
				'href' :  '#view/roomlist', 
				'color' :  window.mvcevent=='roomlist' ? '#000' : '#0000ff',  
				'template' : 'controlLink'
			}; 
	object['href'] += stamp;
    toolbar.roomlink = Factory.write (object);  
// ---------------------------------------------------------------------- *   
    
// ---------------------------------------------------------------------- *   
    object = { 
				'text' : 'Users', 
				'weight' : window.mvcevent=='userlist' ? 'bold' : 'normal', 
//				'onclick' :  window.loggedinuser.PERM_EDIT_USER && window.mvcevent!='userlist' ? 'UserListEvent.invoke()' : '', 
				'onclick' :  'void(0)', 
				'href' :  '#view/userlist', 
				'color' :  window.mvcevent=='userlist' ? '#000' : '#0000ff',  
				'template' : 'controlLink'
			}; 
	object['href'] += stamp;
    toolbar.userlink = Factory.write (object);  
// ---------------------------------------------------------------------- *   
    
// ---------------------------------------------------------------------- *   
//    object = { 
//				'text' : 'Blog', 
//				'weight' : 'normal',  
//				'onclick' :  'void(0)', 
//				'href' :  'http://www.101stratford.com/101/ebp6/wordpress/', 
//				'color' : '#0000ff',  
//				'template' : 'controlLink'
//			}; 
//	object['href'] += stamp;
//    toolbar.userlink = Factory.write (object);  
// ---------------------------------------------------------------------- *  
    
// ---------------------------------------------------------------------- * 
    switch (window.http.readyState)
    {
        case ReadyState.SENDING:
            toolbar.status = 'Contacting server...';
            break;
        case ReadyState.LOADING:
            toolbar.status = 'Message received...';
            break;
        case ReadyState.PROCESSING:
            toolbar.status = window.processmessage;
            break;
    }
	 
// ---------------------------------------------------------------------- *  
    
// ---------------------------------------------------------------------- * 
    toolbar.welcome = ' Welcome, ' + window.loggedinuser.name;
// ---------------------------------------------------------------------- * 

// ---------------------------------------------------------------------- *  
    object = { 
				'text' : 'Settings', 
				'weight' : window.mvcevent=='settingspage' ? 'bold' : 'normal', 
				'onclick' :  'void(0)', 
//				'onclick' :  window.mvcevent=='settingspage' ? '' : 'SettingsPageEvent.invoke()', 
				'href' :  window.mvcevent=='settingspage' ? '' : './#view/settingspage', 
//				'href' : 'javascript::void(0)',
				'color' :  window.mvcevent=='settingspage' ? '#000' : '#0000ff',  
				'template' : 'controlLink'
			}; 
    toolbar.settinglink = Factory.write (object);  
// ---------------------------------------------------------------------- *  
    
// ---------------------------------------------------------------------- *  
    object = { 'text' : 'Sign Out', 
				'weight' : 'normal', 
				'onclick' : 'if(!confirm(\'Exit '+APPLICATION_NAME+'?\')) return false;',  
				'href' :  './', 
				'color' : '#000066', 
				'template' : 'controlLink'
			};
    toolbar.exitlink = Factory.write (object);  
// ---------------------------------------------------------------------- *  

    toolbar.template 	= 'toolBar'; 	
//    toolbar.url = Toolbar.caption ();
	toolbar.nameparam 	= data['nm'] ? data['nm'] : '';
	toolbar.datestart 	= Factory.write ({'id':'esd','value':(data['s1'] ? dateUS(data['s1']) : 'Start Date'),'text':'Start Date','enable':dis,'template':'dateSelect'});
	toolbar.dateend 	= Factory.write ({'id':'eed','value':(data['s2'] ? dateUS(data['s2']) : 'End Date'),'text':'End Date','enable':dis,'template':'dateSelect'});
    
    object = { 'text' : window.searchmode==0 ?  'More &#187;' : 'Less &#171;', 
				'class' : 'small', 
				'onclick' : window.http.readyState == ReadyState.DONE || window.loggedinuser.PERM_BROWSE ? 'ToggleSearch()' : '',  
				'href' : 'javascript::void(0)',
				'color' : window.http.readyState == ReadyState.DONE || window.loggedinuser.PERM_BROWSE ? 'blue' : '#999',  
				'template' : 'controlLink'
			 };
			 
	toolbar.togglelink = '&nbsp;';
    toolbar.togglelink += Factory.write  (object) + '&nbsp;'; 
	
	var dropdown = { };
	var linkHash = {
	            'Properties' : Toolbar.CUSTOM, 
	            'Fields' : Toolbar.INCLUDE, 
	            'Output' : Toolbar.OUTPUT, 
	            'Debug' : Toolbar.EMAIL, 
	            'Event Options' : Toolbar.SORT
	                };   
	toolbar.searchbar = '';
	if (window.searchmode > 0)
	{
		
    	toolbar.searchbar += '		<td valign="bottom" nowrap="nowrap">';		
		for (var e in linkHash)
		{
			object = {
				'text' : e + ' ' + (linkHash[e] & window.searchmode?'&#171;':'&#187;'), 
				'class' : 'small', 
				'onclick' :  'Toolbar.set(' + linkHash[e] + ')', 
				'color' :  linkHash[e] & window.searchmode ? '#666' : '#0000ff',  
				'href' : 'javascript::void(0)',
				'template' : 'controlLink'
				};
			toolbar.searchbar += ' | ' + Factory.write (object);
		} 		
    	toolbar.searchbar += '		</td>';
    	toolbar.searchbar += '	</tr>';
		
		object = Factory.empty ('optionRow'); 
		if (Toolbar.ROOM & window.searchmode)
		{ 
			dropdown = { 'id' : 'usr', 
						'collection' : window.users, 
						'key' : 'ID',
						'selected' : data['us'],
						'transformer' : function (x) { return x.LastName + ', ' + x.FirstName; }
						}   
			object.content = '';
			object.content += ' Room: <select id=spr><option value=0>(Choose room)' + RoomDropdown (0, data['rm'], '') + '</select>';
			object.content += ' &nbsp;&nbsp;Created by: ' + Toolbar.selector (dropdown);  
			
			toolbar.searchbar += object.draw(); 
		}
// ---------------------------------------------------------------------- *  
		if (Toolbar.EMAIL & window.searchmode)
		{  
			dropdown = { 'id' : 'sq', 
						'onchange' : "window.selectedquery=this.value;Controller.Reload()", 
						'collection' : window.request.query, 
						'key' : 'url', 
						'name' : 'action', 
						'selected' : window.selectedquery
						}  
    	    object.content = '<b>Debug info</b>&nbsp; bytes loaded: ('+TOTALBYTES+' b), saved queries: '; 
			object.content += Toolbar.selector (dropdown); 
			if (window.selectedquery)
			    object.content += ' <a href="javascript:void(0)" onclick="messagebox(\'' + window.selectedquery + '\')">' + window.selectedquery + '</a>'; 
    	    
			dropdown = { 'id' : 'tm', 
						'onchange' : "window.selectedtm=this.value;Controller.Reload()", 
						'collection' : Factory, 
						'key' : 'name', 
						'name' : 'name', 
						'selected' : window.selectedtm
						}  
    	    object.content += ', Templates ';
			object.content += Toolbar.selector (dropdown); 
    	    object.content += ' <input type=button onclick="dialog (Factory[tm.value].text,tm.value)" value="Show"> ';
    	     
			toolbar.searchbar += object.draw();
		}
// ---------------------------------------------------------------------- * 
		if (Toolbar.SORT & window.searchmode)
		{  
			dropdown = { 'id' : 'qcl', 
						'onchange' : "window.selectedcalendar=this.value;Controller.Reload()", 
						'collection' : window.calendars, 
						'key' : 'id', 
						'name' : 'calendar_name', 
						'selected' : window.selectedcalendar
						}  
    	    object.content = 'Calendar '; 
			object.content += Toolbar.selector (dropdown); 
			
			dropdown = { 'id' : 'qct', 
						'onchange' : "window.selectedcategory=this.value;Controller.Reload()", 
						'collection' : window.folders, 
						'key' : 'id', 
						'name' : 'title', 
						'selected' : window.selectedcategory
						}  
    	    object.content += '&nbsp;&nbsp;Category '; 
			object.content += Toolbar.selector (dropdown);  
			
			dropdown = { 
			            'id' : 'adf', 
						'onchange' : "Controller.AlsoField=this.value;DebugEventDataGrid.invoke ();", 
						'collection' : [{'key':'Comments','name':'Set up'}], 
						'key' : 'key', 
						'name' : 'name', 
						'selected' : Controller.AlsoField
						}  
    	    object.content += '&nbsp;&nbsp;Also search in: '; 
			object.content += Toolbar.selector (dropdown);  
			toolbar.searchbar += object.draw();
		}
// ---------------------------------------------------------------------- *  
		if (Toolbar.CUSTOM & window.searchmode)
		{  
			dropdown = { 
			            'id' : 'sprnm', 
						'collection' : window.properties, 
						'key' : 'id',
						'onchange' : 'Toolbar.selected=this.value;Controller.Reload();',
						'selected' : Toolbar.selected,
						'name' : 'property_name'
						}   
			object.content = 'Value of:&nbsp;&nbsp;&nbsp;'
			object.content += Toolbar.selector (dropdown); 
			object.content += ' is ';
			
			if (Toolbar.selected > 0)
			{
				var pt=GetCustomPropertyById(Toolbar.selected);
				if ( pt.type_name=='list'||pt.type_name=='dropdown')
				{ 
					dropdown = { 
					            'id' : 'sprvl', 
								'collection' : pt.valuelist, 
								'key' : null, 
								'selected' : data['pv']
								}  
					object.content += Toolbar.selector (dropdown);
				}
				else
				{
					object.content += '<input id="sprvl" class="flat" value="' + (data['pv'] ? data['pv'] : '') + '"/>';
				}
			}
			else
			{
				object.content += '[no property selected]';
			}  
			
			toolbar.searchbar += object.draw();  
		}
// ---------------------------------------------------------------------- * 
		if (Toolbar.INCLUDE & window.searchmode)
		{ 
			dropdown = { 
			            'onchange' : "Controller.SortField=this.value;Controller.Reload ();", 
						'collection' : EventObject.params(),  
						'ordinal' : true,
						'selected' : Controller.SortField
						}  
			var table = Factory.empty ('fieldForm');
			table.fields = '';
			table.fieldlist = Toolbar.fieldlist (Controller.IncludedFields, 'Controller.IncludedFields=bite(Controller.IncludedFields, parseInt(this.value));DebugEventDataGrid.invoke ();');
			table.selector = Toolbar.selector (dropdown);
			object.content = table.draw(); 
			toolbar.searchbar += object.draw();   
		}
// ---------------------------------------------------------------------- * 
		if (Toolbar.OUTPUT & window.searchmode)
		{ 
    	    object.content = 'Return as: ';  
			object.content += '<select>';
			object.content += '<option value="list">List View'; 
			object.content += '<option value="report">Expanded View'; 
			object.content += '</select>';
			object.content += '&nbsp;&nbsp;';
			object.content += 'Send email copy to: ';
			object.content += '<input size=40 class="flat" id="mto" value="' + (Controller.MailTo?Controller.MailTo:'') + '"/>';
			toolbar.searchbar += object.draw();   
		} 
// ---------------------------------------------------------------------- * 
		
    	toolbar.searchbar += '<tr>';
		toolbar.searchbar += '<td align=right>&nbsp;</td>';
    	toolbar.searchbar += '<td colspan=3 nowrap class=small>';
    	toolbar.searchbar += '&nbsp;</td>';
	} 
    
// ---------------------------------------------------------------------- * 
	toolbar.viewtab = '';
    if (window.mvcevent=='eventlist' && window.http.readyState == ReadyState.DONE && window.loggedinuser.PERM_BROWSE)
    {
        toolbar.viewtab += '<table bgcolor="#c9d7f1" cellspacing=0 cellpadding=5><tr>';
        toolbar.viewtab += '<td bgcolor=white nowrap> '; 
        toolbar.viewtab += '<a href="javascript:void(0)" onclick="SectorPrint (\'dcelisttable\')" style="font-size:9pt">';
        toolbar.viewtab += '<img src="/webservices/images/explorer/i.p.printv.gif" align="absmiddle" border="0"></a>';
        toolbar.viewtab += ' <a href="javascript:void(0)" onclick="SectorPrint (\'dcelisttable\')" style="font-size:9pt">Print</a>';
        toolbar.viewtab += '</td>';
        if (window.gridview==dgrid.EXPANDED) toolbar.viewtab += '<td nowrap style="border-top:solid 1px #e0e0e0;border-right:solid 1px #e0e0e0" align="center" class=black>Expanded View</td>';
        else toolbar.viewtab += '<td nowrap bgcolor="#96b4c0" align="center" style="border-bottom:solid 1px white;border-top:solid 1px white"><a title="Switch to Expanded View" href="javascript:void(0)" onclick="EventDataGridChangeMode(\'' + dgrid.EXPANDED + '\')" class="white">Expanded View</a></td>';
        if (window.gridview==dgrid.LIST) toolbar.viewtab += '<td nowrap style="border-left:solid 1px white;border-top:solid 1px #e0e0e0" align="center"class=black>List View</td>';
        else toolbar.viewtab += '<td nowrap style="border-bottom:solid 1px white;border-top:solid 1px white" bgcolor="#96b4c0" align="center"><a title="Switch to List View" href="javascript:void(0)" onclick="EventDataGridChangeMode(\'' + dgrid.LIST + '\')" class="white">List View</a></td>';
        toolbar.viewtab += '</tr></table>';
    }
// ---------------------------------------------------------------------- * 
	htm +=  Factory.write (toolbar); 
    return htm;
}   
function pb(x) { return parseInt(x,2); }
function Toolbar () { }
Toolbar.MAIN 		= pb('000000');
Toolbar.ROOM 		= pb('000001');
Toolbar.CUSTOM 		= pb('000010');
Toolbar.INCLUDE 	= pb('000100');
Toolbar.SORT 		= pb('001000');
Toolbar.OUTPUT 		= pb('010000');
Toolbar.EMAIL 		= pb('100000');
Toolbar.status 		= Toolbar.MAIN; 
Toolbar.selected	= -1;
Toolbar.set = function (x) 
{ 
    window.searchmode=bite(window.searchmode,x); 
    Controller.Reload(); 
    setCookie('searchmode', window.searchmode); 
    window.resizer.invoke(); 
}
Toolbar.read = function (url)
{
	var address	= url || Controller.LastListURL; 
	if (! address ) return new Array();
	var line 	= address.replace (DATA_URL, ''); 
	var array 	= line.split ('/');
	var result 	= new Array();
	for (var x=0;x<array.length;x+=2)
	{
		result [array[x]]=array[x+1];
	}
	return result;
}

Toolbar.caption = function ()
{
	if (!Controller.LastListURL) return new Array();
	
	
	if (window.report)
	{
		return window.reports[window.report].title;
	}
	 
	
	
	var line 	= Toolbar.read ();
	var result  = '';
	var parse   = dateInput;
	
	if (line['action']=='event') result += 'All events';
	else if (line['action']=='next5') result += 'Next 10 events';
	if (line['s2']) result += ' between ' + parse(line['s1']) + ' and ' + parse(line['s2']);
	else if (line['s1']) result += ' on ' + parse(line['s1']) + ' ';
	if (line['nm']) result += ' named "' + line['nm'] + '" '; 
	if (line['af']) result += ' or set up is "' + line['nm'] + '" ';
	if (line['rm']) result += ' in "<a href="#report/search/action/event/op/xms/rm/'+line['rm']+'">' + GetRoomName(line['rm']) + '</a>" ';
	if (line['us']) result += ' created by <a href="#report/byuser/id/'+line['us']+'">' + GetUserName(line['us']) + '</a> ';
	if (line['pn']) result += ' where ' + line['pn'] + ' is "' + line['pv'] + '" ';
	
    document.title = APPLICATION_NAME + ' - ' + result; 
	return result; 
}
Toolbar.controlLink = '<a style="color:(color);font-weight:(weight)" onclick="(onclick)" class="(class)" href="(href)"><cn:text /></a>';
Toolbar.optionRow = '<tr><td align=right>&nbsp;</td><td colspan=4 style="padding-top:2px">(content)</td></tr>'; 
Toolbar.fieldForm = '';  
Toolbar.fieldForm += '<table cellspacing=0 cellpadding=0>';
Toolbar.fieldForm += '	<tr>';
Toolbar.fieldForm += '		<td >';
Toolbar.fieldForm += '			Included Fields: ';
Toolbar.fieldForm += '		</td>';
Toolbar.fieldForm += '		<td>';
Toolbar.fieldForm += '			<table>';
Toolbar.fieldForm += '				<tr>(fieldlist)';
Toolbar.fieldForm += '					<td colspan="5">'; 
Toolbar.fieldForm += '						Sort by: (selector)'; 
Toolbar.fieldForm += '					</td>'; 
Toolbar.fieldForm += '				</tr>';
Toolbar.fieldForm += '			</table>';  
Toolbar.fieldForm += '		</td>';
Toolbar.fieldForm += '	</tr>';
Toolbar.fieldForm += '</table>'; 
			
Number.prototype.toBinary = function ()
{
    var i=0;
    var str='';
    while (Math.pow(2,i) < this || Math.pow(2,i) < 64)
    {
        str = ((Math.pow(2,i) & this) ? '1' : '0') + str;
        i ++;
    }
    return str;
}

var dropDown = function () { return { 'draw' : function () { return Toolbar.selector(this) } } };

Toolbar.selector = function (o)
{
	var htm='';
	htm += '<select ';
	if (o.id) htm += ' id="' + o.id + '" ';
	if (o.onchange) htm += ' onchange="' + o.onchange + '"';
	htm += '>';
	htm += '<option value="0">(Choose)';
	for (var v in o.collection)
	{
	    if (typeof(o.collection[v])=='object' || typeof(o.collection[v])=='string')
	    {
		    htm += '<option '; 
		    if (o.key) 
		    {
			    htm += o.collection[v][o.key] == o.selected ? 'selected' : ''; 
			    htm += ' value="' + o.collection[v][o.key] + '"';
		    }
		    else if (o.ordinal) 
		    {
			    htm += v == o.selected ? 'selected' : ''; 
			    htm += ' value="' + v + '"';
		    }
		    else
		    {
			    htm += o.collection[v] == o.selected ? 'selected' : ''; 
			    htm += ' value="' + o.collection[v] + '" ' ;
		    }
		    htm += '>';
		    if (o.transformer) htm += '' + o.transformer(o.collection[v]) + '';
		    else if (o.key) htm += '' + o.collection[v][o.name]; 
		    else htm += '' + o.collection[v]; 
		    htm += '</option>';
		}
	} 
	htm += '</select>'; 
	return htm;
}


Toolbar.fieldlist = function (checked, command)
{ 
    var htm='';
	var binary;
	var k=0;
	var searchfield=EventObject.params();
	for (var e in searchfield)
	{ 
		binary = Math.pow(2, k);
		htm += '<td style="color:#666" nowrap><input ';
		htm += ' ' + (checked & binary ? ' checked ' : '');
	//	htm += ' ' + (Controller.DefaultFields & binary ? ' readonly ' : '');
	//	if (! (Controller.DefaultFields & binary)) 
		    htm += ' onclick="' + command + '"';
		htm += ' type="checkbox" value="' + binary + '">' + searchfield[e] + '</td>'; 
		if ((k+1)%5==0)
		{
			htm += '</tr><tr>';
		}
		k++;
	} 
	return htm;
}

